package org.zeuzgroup.fight.rappelz;

import java.awt.Color;
import java.awt.Point;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

import org.slf4j.Logger;
import org.zeuzgroup.console.Console;

public class Fight {

	private Robot robot;

	static final Logger log = org.slf4j.LoggerFactory.getLogger(Console.class);

	private int fights = 100;

	public Fight(Robot robot) {
		this.robot = robot;

		
		while (hasMoreFights()) {
			logOff(robot);
			fight(robot);
		}
		quitGame();

	}


	private void quitGame() {
		log.info("Quiting Game!");
		getSystemMenu();
		clickQuit();
		clickConfirm();
		

		
	}

	private void clickConfirm() {
		moveAndClick(new Point(450,215));
		//450,215 (confirm)
		
	}

	private void moveAndClick(Point p) {
		robot.mouseMove(p.x, p.y);
		robot.mousePress(InputEvent.BUTTON1_MASK);
		robot.delay(50);
		robot.mouseRelease(InputEvent.BUTTON1_MASK);
	}

	private void clickQuit() {
		moveAndClick(new Point(505,450));
		//505,450 (quit)
		
	}

	private void getSystemMenu() {
		keyPressAndRelease(robot, KeyEvent.VK_CONTROL, KeyEvent.VK_Z);
		
	}

	private void fight(Robot robot) {
		pickFight(robot);
		attackAndKill(robot);
		pickupLoot();
		deductOneFight();
	}

	private void deductOneFight() {
		log.info("deductOneFight");
		fights--;
	}

	private void pickFight(Robot robot) {
		log.info("pickFight");
		keyPressAndRelease(robot, KeyEvent.VK_TAB);
	}

	private void attackAndKill(Robot robot) {
		log.info("attackAndKill");
		keyPressAndRelease(robot, KeyEvent.VK_F2);
		while (opponentHasMoreHealth(robot)) {
			robot.delay(500);
		}

	}

	private boolean opponentHasMoreHealth(Robot robot) {
		// TODO Auto-generated method stub
		// full hp 685x,y28
		// 0 hp 486x,y28
		// filled rgb (161,92,61);
		// empty rgb (34,20,17)
		// if(r>100) then full else empty
		int health = scanHealth(robot, 5, new Point(486, 28),
				new Point(685, 28));
		log.info("Creature has " + health + "% health");
		return health > 0;
	}

	/**
	 * returns number from 0-100 depending on hit bar
	 * 
	 * @param robot
	 * @return
	 */
	private int scanHealth(Robot robot, int divides, Point start, Point end) {

		int health = 0;

		int range = (int) (end.getX() - start.getX());

		for (int i = 0, percent = 0; percent <= 100; i++, percent = i
				* (100 / divides)) {
			int currentOffset = (range * percent) / 100;
			Color color = robot.getPixelColor(start.x + currentOffset, start.y);
			log.info("Scan Health r:"+color.getRed()+" g:"+color.getGreen()+" b:"+color.getBlue() );
			if (color.getRed() > 100) {
				health = percent;
			} else {
				break;
			}
		}
		return health;
	}

	private boolean hasMoreFights() {
		return fights > 0;
	}

	private void logOff(Robot robot) {
		robot.mouseMove(60, 710);
		robot.mousePress(InputEvent.BUTTON1_MASK);
		robot.delay(500);
		robot.mouseRelease(InputEvent.BUTTON1_MASK);
		robot.delay(500);
		robot.mousePress(InputEvent.BUTTON3_MASK);
		robot.delay(500);
		robot.mouseRelease(InputEvent.BUTTON3_MASK);
		log.info("Loggin off");

	}

	private void keyPressAndRelease(Robot robot, int... event) {

		for (int i = 0; i < event.length; i++) {
			robot.keyPress(event[i]);
			robot.delay(50);

		}
		robot.delay(500);

		for (int i = 0; i < event.length; i++) {
			robot.keyRelease(event[i]);
		}

	}

	private boolean pickupLoot() {
		// pickup loot
		log.info("pickupLoot");
		keyPressAndRelease(robot, KeyEvent.VK_F2);
		robot.delay(5000);
		keyPressAndRelease(robot, KeyEvent.VK_F2);
		robot.delay(5000);
		keyPressAndRelease(robot, KeyEvent.VK_F2);
		return true;
	}

}
